package cn.nexttec.noteplayer.ui.reflow

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import cn.nexttec.noteplayer.databases.dao.NoteDao
import cn.nexttec.noteplayer.injection.IoDispatcher
import cn.nexttec.noteplayer.injection.MainDispatcher
import cn.nexttec.noteplayer.models.NoteFile
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import javax.inject.Inject
import javax.inject.Named

@HiltViewModel
class NoteViewModel @Inject constructor(
    @Named("BaseDir") val notePath: File,
    val noteDao: NoteDao,
    @IoDispatcher val ioDispatcher: CoroutineDispatcher,
    @MainDispatcher val mainDispatcher: CoroutineDispatcher
) : ViewModel() {
    private var page: Int = 1
    private val offset: Int
        get() = (page - 1) * 10
    private val _notes = MutableLiveData<List<NoteFile>>()
    val notes get() = _notes
    fun loadNotes() {
        viewModelScope.launch(ioDispatcher) {
            val list = noteDao.getNotes(10, offset)
            withContext(mainDispatcher) {
                list?.let{
                    _notes.value = it
                }

            }
        }

    }

    private val _text = MutableLiveData<String>().apply {
        value = "This is reflow Fragment"
    }
    val text: LiveData<String> = _text
}